/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.MaquinariaFacade;
import VO.ConductoresVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.conductoresBean;

/**
 *
 * @author Alan
 */
public class ListaConductoresAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private final static String LOAD = "load";
    private final static String VOLVER = "volver";
    

    public ActionForward cargarMaquinarias(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MaquinariaFacade conex = new MaquinariaFacade();

        Vector listaMaquinas = conex.cargarNomenclaturas();
        request.setAttribute("listaMaquinas", listaMaquinas);
        return mapping.findForward(LOAD);
    }

    public ActionForward CargarConductores(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        conductoresBean frm = (conductoresBean)form;
        String maquinaria = frm.getNomenclatura();

        MaquinariaFacade conex = new MaquinariaFacade();

        Vector listaMaquinas = conex.cargarNomenclaturas();
        Vector listaConductores = conex.cargarConductores(maquinaria);
        Vector listaNoConductores = conex.cargarNoConductores(maquinaria);

        request.setAttribute("listaMaquinas", listaMaquinas);
        request.setAttribute("listaConductores", listaConductores);
        request.setAttribute("listaNoConductores", listaNoConductores);
        request.setAttribute("maquinaSeleccionada", maquinaria);

        return mapping.findForward(LOAD);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=Registrar,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward Registrar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        conductoresBean frm = (conductoresBean)form;
        String maquinaria = frm.getNomenclaturaSelec();
        ConductoresVO listaCond = new ConductoresVO();
        MaquinariaFacade conex = new MaquinariaFacade();


        //**********************************************************************
        String cond[];
        Vector conductores = new Vector(),maqsVector=new Vector();

        cond = request.getParameterValues("cond");

        cond= cond[0].split(",");

        for(int i=1;i<cond.length;i++)
          conductores.add(cond[i]);

        listaCond.setConductores(conductores);
        listaCond.setMaquinaria(maquinaria);

        boolean resultado = conex.registrarConductores(listaCond);
        if(resultado == true){
            String aviso = "<span style='color:green;font-weight:bold'>Conductores registrados Exitosamente</span>";
            request.setAttribute("aviso",aviso);

            Vector listaMaquinas = conex.cargarNomenclaturas();
            Vector listaConductores = conex.cargarConductores(maquinaria);
            Vector listaNoConductores = conex.cargarNoConductores(maquinaria);

            request.setAttribute("listaMaquinas", listaMaquinas);
            request.setAttribute("listaConductores", listaConductores);
            request.setAttribute("listaNoConductores", listaNoConductores);
            request.setAttribute("maquinaSeleccionada", maquinaria);

            return mapping.findForward(SUCCESS);
        }
        else{
            String aviso = "<span style='color:green;font-weight:bold'>Error al registrar</span>";
            request.setAttribute("aviso",aviso);
            return mapping.findForward(FAILURE);
        }
        //********************************************************************   
    }
     public ActionForward Volver(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception{
            return mapping.findForward(VOLVER);
    }
}